import math

import pandas as pd
import numpy as np


class Geography():
    Dic = {}
    def staticdata(self, data):  # 标准数据处理
        dic = {}  # 存储json的字典
        column = data[data.columns[0]].values  # 分类
        index = data.columns[1:].values  # 年份

        for i in index:
            item = data[i]
            # print(item)
            count = 0
            li = []  # 存储每个省数据（字典）的列表

            for j in column:

                li.append({j: item.values.tolist()[count]})  # 加入一年各个数据的字典
                count += 1
            dic[int(i[:-1])] = li



        return dic

    def staticprovince(self, data):  # 标准分省数据处理
        dic = {}
        index = data["地区"]
        column = data.columns[1:]
        # print(column)
        for i in index:
            item = data[data["地区"] == i]  # 每个省的数据
            item.index = [i]  # 设置列名
            li = []  # 存储每个省数据（字典）的列表
            # print(item.values[0][1:])
            count = 1
            for j in column:
                if math.isnan(item.values[0][count]):
                    continue
                value = item.values[0][count]
                li.append({j: value})
                count += 1
            dic[i] = li
        return dic

    def country(self):
        country = pd.read_csv("../data/temp/分省乡村人口数据.csv", index_col=0, encoding='gbk')
        self.Dic["country"] = self.staticprovince(country)

    def urban(self):
        urban = pd.read_csv("../data/temp/分省城镇人口数据.csv", index_col=0, encoding='gbk')
        self.Dic["urban"] = self.staticprovince(urban)

    def resident_city(self):
        resident = pd.read_csv("../data/temp/分省常驻人口数据.csv", index_col=0, encoding='gbk')
        self.Dic["resident_city"] = self.staticprovince(resident)

    def resident_year(self):
        resident = pd.read_csv("../data/temp/分省常驻人口数据.csv", index_col=0, encoding='gbk')
        self.Dic["resident_year"] = self.staticdata(resident)



    def migration(self):
        migration = pd.read_csv("../data/temp/迁入迁出人口数据.csv", index_col=0, encoding='gbk')
        self.Dic["migration"] = self.staticprovince(migration)

    def migration_new(self):
        migration = pd.read_csv("../data/temp/迁入迁出人口数据.csv", index_col=0, encoding='gbk')
        self.Dic["migration_new"] = self.staticdata(migration)

    def density(self):
        density = pd.read_csv("../data/temp/分省人口密度.csv", index_col=0, encoding='gbk')
        self.Dic["density"] = self.staticprovince(density)

    def density_new(self):
        density = pd.read_csv("../data/temp/分省人口密度.csv", index_col=0, encoding='gbk')
        self.Dic["density_new"] = self.staticdata(density)

    def urbanization(self):
        urbanization = pd.read_csv("../data/temp/分省城镇率数据.csv", index_col=0, encoding='gbk')
        self.Dic["urbanization"] = self.staticprovince(urbanization)


    def urbanization_new(self):
        urbanization = pd.read_csv("../data/temp/分省城镇率数据.csv", index_col=0, encoding='gbk')
        self.Dic["urbanization_new"] = self.staticdata(urbanization)

    def getdata(self):
        self.country()
        self.urban()
        self.resident_city()
        self.resident_year()
        self.migration()
        self.density()
        self.urbanization()
        self.density_new()
        self.migration_new()
        self.urbanization_new()


# 测试
# g = Geography()
# g.getdata()
# print(g.Dic["density"]["北京市"])
# print(g.Dic["urbanization"]["北京市"])
# print(g.Dic["resident_year"][2018])
# print(g.Dic["country"]["北京市"])
# print(g.Dic["urban"]["北京市"])
# print(g.Dic["migration"]["北京市"])
